package com.jt.gateway.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.reactive.CorsWebFilter;
import org.springframework.web.cors.reactive.UrlBasedCorsConfigurationSource;
import org.springframework.web.util.pattern.PathPatternParser;

@Configuration
public class CrosFilterConfig{
 @Bean
 public CorsWebFilter corsFilter(){
  System.out.println("corsWebFilter()");
  UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource(new PathPatternParser());
  CorsConfiguration corsConfiguration = new CorsConfiguration();
  //1.配置跨域
  //允许哪种请求头跨域
  corsConfiguration.addAllowedHeader("*");
  //允许哪种方法类型跨域 get post delete put
  corsConfiguration.addAllowedMethod("*");
  // 允许哪些请求源跨域
  corsConfiguration.addAllowedOrigin("*");
  // 是否携带cookie跨域
  corsConfiguration.setAllowCredentials(true);
  //允许跨域的路径
  source.registerCorsConfiguration("/**",corsConfiguration);
  return new CorsWebFilter(source);
 }
}
